/**
 * Copyright (C), 2015-2021, 开度
 * FileName: CreateTableConfig
 * Author:   ASUS
 * Date:     2021/5/8 14:28
 * Description: 用于测试自动生成数据库表字段
 * History:
 * <author>          <time>          <version>          <desc>
 * Hezeyu           2021/5/8           1.0              用于测试自动生成数据库表字段
 */
package com.example.spring.security.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * 〈用于测试自动生成数据库表字段〉
 *
 * @author ASUS
 * @create 2021/5/8
 * @since 1.0.0
 */
@Component
public class CreateTableConfig {

    @Value(value = "${spring.datasource.driver-class-name}")
    private String driver;

    @Value(value = "${spring.datasource.url}")
    private String url;

    @Value(value = "${spring.datasource.username}")
    private String userName;

    @Value(value = "${spring.datasource.password}")
    private String password;

    public Connection init() throws SQLException, ClassNotFoundException {
        //连接数据库
        Class.forName(driver);
        //测试url中是否包含useSSL字段，没有则添加设该字段且禁用
        if( url.indexOf("?") == -1 ){
            url = url + "?useSSL=false" ;
        }
        else if( url.indexOf("useSSL=false") == -1 || url.indexOf("useSSL=true") == -1 )
        {
            url = url + "&useSSL=false";
        }
        Connection conn = DriverManager.getConnection(url, userName, password);
        return conn;
    }

}
